<?php

namespace app\admin\controller;

use app\common\entity\BackResult;
use app\common\service\TopicService;
use app\common\service\VideoService;
use app\Request;

class Topic extends Base
{
    /**
     * 默认无关联视屏id
     * @var int
     */
    private $noRelationVideo = 0;
    /**
     * 默认不免费
     * @var int
     */
    private $defaultFree = 0;

    /**
     * 专栏列表
     * @param Request $request
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index(Request $request)
    {
        $page = $request->param("page", 1);
        $pageSize = $request->param('pageSize', 10);
        $searchField = $request->param('search_field', '');
        $searchValue = $request->param('search_value', '');
        $status = $request->param('status', 0);
        $service = new TopicService();
        $data = $service->topicIndex($page, $pageSize, $searchField, $searchValue, $status);
        return BackResult::success($data);
    }

    /**
     * 设置视频排序
     * @param Request $request
     * @return \think\Response
     * @throws \app\common\exception\BackErrorException
     */
    public function setSort(Request $request)
    {
        $id = $request->param("id"); //视频id
        $sort = $request->param("sort", '9999'); //视频排序
        $service = new VideoService();
        $service->setSort($id, $sort);
        return BackResult::success();
    }

    public function create(Request $request)
    {
        $data = $request->post();
        $service = new TopicService();
        $data = $service->create($data);
        return BackResult::success($data);
    }

    public function update(Request $request)
    {
        $id = $request->param('id');
        $data = $request->post();
        $service = new TopicService();
        $data = $service->update($data, $id);
        return BackResult::success($data);
    }

    /**
     * 上架专栏
     * @param Request $request
     * @return \think\Response
     */
    public function shelves(Request $request)
    {
        $id = $request->param("id");
        if (empty($id)) {
            return BackResult::error([], "缺少id");
        }
        $data = (new TopicService())->shelvesTopic($id);
        return BackResult::success($data);
    }

    /**
     * 下架专栏
     * @param Request $request
     * @return \think\Response
     * @throws \app\common\exception\BackErrorException
     */
    public function remove(Request $request)
    {
        $id = $request->param('id');
        $service = new TopicService();
        $service->remove($id);
        return BackResult::success();
    }

    public function one(Request $request)
    {
        $id = $request->param("id");
        if (empty($id)) {
            return BackResult::error([], "缺少id");
        }
        $service = new TopicService();
        $data = $service->getOne($id);
        return BackResult::success($data);
    }

    /**
     * 获取关联视频列表
     * @param Request $request
     * @param VideoService $videoService
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function videoList(Request $request, VideoService $videoService, TopicService $topicService)
    {
        $id = $request->param("id");
        $this->checkTopic($id, $topicService);
        $videoList = [];
        $videoResult = $videoService->getVideoByTopicId($id);
        if ($videoResult) {
            $videoList = $videoResult->toArray();
        }
        return BackResult::success(['list' => $videoList]);
    }

    /**
     * 设置关联视频
     * @param Request $request
     * @param VideoService $videoService
     * @param TopicService $topicService
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function videoRelation(Request $request, VideoService $videoService, TopicService $topicService)
    {
        $id = $request->param("id");
        $this->checkTopic($id, $topicService);
        $data = $request->post();
        $data['topic_id'] = $id;
        if (!isset($data['video_id']) || empty($data['video_id'])) {
            return BackResult::error([], "至少选择一个视频关联");
        }
        if (!isset($data['fee']) || empty($data['fee'])) {
            $data['fee'] = 0.00;
        }
        $videoRes = $videoService->updateFeeAndTopic($data);
        if (!$videoRes) {
            return BackResult::error([], "视频信息有误！");
        }
        return BackResult::success(['result' => true]);
    }

    /**
     * 删除关联视频
     * @param Request $request
     * @param VideoService $videoService
     * @param TopicService $topicService
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function videoRelieve(Request $request, VideoService $videoService, TopicService $topicService)
    {
        $id = $request->param("id");
        $this->checkTopic($id, $topicService);
        $data = $request->post();
        $data['topic_id'] = $id;
        if (!isset($data['video_id']) || empty($data['video_id'])) {
            return BackResult::error([], "至少选择一个视频关联");
        }
        $update = ['topic_id' => $this->noRelationVideo, 'fee' => 0.00];
        $res = $videoService->updateTopic($data, $update);
        if (!$res) {
            return BackResult::error([], "视频信息有误或关联视频不存在");
        }
        return BackResult::success(['result' => true]);
    }

    /**
     * 设置免费视频
     * @param Request $request
     * @param VideoService $videoService
     * @param TopicService $topicService
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function videoFree(Request $request, VideoService $videoService, TopicService $topicService)
    {
        $id = $request->param("id");
        $this->checkTopic($id, $topicService);
        $data = $request->post();
        $data['topic_id'] = $id;
        if (!isset($data['video_id']) || empty($data['video_id'])) {
            return BackResult::error([], "至少选择一个视频关联");
        }
        $update = ['topic_id' => $id, 'is_free' => $data['is_free'] ?? $this->defaultFree];
        $res = $videoService->updateTopic($data, $update);
        if (!$res) {
            return BackResult::error([], "视频信息有误或关联视频不存在");
        }
        return BackResult::success(['result' => true]);
    }

    /**
     * 参数检查
     * @param $id
     * @param $topicService
     * @return bool|\think\Response
     */
    private function checkTopic($id, $topicService)
    {
        if (empty($id)) {
            return BackResult::error([], "缺少id");
        }
        $checkTopic = $topicService->getOne($id);
        if (!$checkTopic) {
            return BackResult::error([], "专栏信息有误！");
        }
        return true;
    }
}